package com.qf.rbac2202.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.rbac2202.sys.entity.SysMenu;
import com.qf.rbac2202.sys.entity.SysRoleMenu;
import com.qf.rbac2202.sys.entity.SysUser;
import com.qf.rbac2202.sys.entity.SysUserRole;
import com.qf.rbac2202.sys.service.ISysMenuService;
import com.qf.rbac2202.sys.service.ISysRoleMenuService;
import com.qf.rbac2202.sys.service.ISysUserRoleService;
import com.qf.rbac2202.sys.service.ISysUserService;
import com.qf.rbac2202.utils.Constons;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CustomRealm extends AuthorizingRealm {

    @Autowired
    ISysUserService userService;

    @Autowired
    ISysMenuService sysMenuService;

    @Autowired
    ISysUserRoleService userRoleService;
    @Autowired
    ISysRoleMenuService roleMenuService;
    /**
     * 获取权限信息
     * @param principal
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        SysUser user = (SysUser) principal.getPrimaryPrincipal();

        //查询user权限字符串
        if (user.getUserId().equals(Constons.SUPER_ADMIN_ID)){

            List<SysMenu> menuList = sysMenuService.list();

            List<String> permToList = menuList.stream()
                    .filter(sysMenu -> sysMenu.getPerms() != null)
                    .map(sysMenu -> sysMenu.getPerms())
                    .collect(Collectors.toList());

            //处理数据库字段中的权限字符串之间的逗号set
            HashSet<String> permSet  =  new HashSet<>();
            for (String pers : permToList) {
                if (pers.contains(",")) {
                    String[] split = pers.split(",");
                    permSet.addAll(Arrays.asList(split));
            }
            }


            simpleAuthorizationInfo.addStringPermissions(permSet);

            return simpleAuthorizationInfo;

        }else {
            List<String> queryPermList = sysMenuService.queryPermList(user.getUserId());

            simpleAuthorizationInfo.addStringPermissions(queryPermList);

            return simpleAuthorizationInfo;


        }
    }

    /**
     * 获取身份信息
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String username = (String) token.getPrincipal();

        //根据用户名查询用户信息
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("username",username);

        SysUser user = userService.getOne(queryWrapper);

        if(user == null){
            throw new AuthenticationException();
        }

        //取出密码返回对象
        String password = user.getPassword();

        return  new SimpleAuthenticationInfo(user,
                password,
                ByteSource.Util.bytes(user.getSalt()),
                this.getName());
    }
}
